import { ref, inject } from "vue";
import { useLoginUserStore } from "../../../store";
import { useOrgStore } from '../../../store/useOrgStore'

export const useOrgTable = (currentPage: any, pageSize: any) => {
    //获取API配置
    const API = inject("API") as any;
    const ApiConfig = inject("ApiConfig") as any;

    const total = ref(0);
    const tableData = ref([]);
    const orgId = ref('');
    const orgName = ref("");
    const orgType = ref(null);

    const loginUserStore = useLoginUserStore();
    const orgStore = useOrgStore();
    orgId.value = orgStore.selectedOrgNode == null ? loginUserStore.currentUser.orgId : orgStore.selectedOrgNode.id;

    //查询
    const fetchData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Org.GetAllAndChild,
                {
                    pageIndex: currentPage.value,
                    pageSize: pageSize.value,
                    condition: { id: orgId.value, orgName: orgName.value, orgType: orgType.value },
                }
            );

            if (response.status == "200" && response.data.code == "200") {
                tableData.value = response.data.data;
                total.value = response.data.count;
            }
        } catch (error) {
            console.error(error);
        }
    };
    //新增
    const addData = async (orgModel) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Org.Create,
                { 'OrgName': orgModel.orgName, 'Pid': orgModel.pid, 'OrgType': orgModel.orgType, 'Mark': orgModel.mark }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //更新
    const updateData = async (orgModel) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Org.Update,
                { 'Id': orgModel.id, 'OrgName': orgModel.orgName, 'Pid': orgModel.pid, 'OrgType': orgModel.orgType, 'Mark': orgModel.mark }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //删除
    const deleteData = async (orgId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Org.DeleteByOrgId,
                { 'orgId': orgId }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }

    return { orgId, orgName, orgType, total, tableData, fetchData, addData, updateData, deleteData };
};
